package com.chixing.service.impl;

import com.chixing.mapper.*;
import com.chixing.pojo.*;
import com.chixing.service.ReviewService;
import com.chixing.util.MongodbUtils;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ReviewServiceImpl implements ReviewService {

    public static final String COLLECTIONNAME_REVIEW = "review";


    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private MyorderMapper myorderMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public ServerResult  insert(BasePojo Review) {
        Object rows = MongodbUtils.insert(Review, COLLECTIONNAME_REVIEW);
        System.out.println(rows);
        return ServerResult.saveSussess(rows);
    }

    @Override
    public ServerResult order2review(String OrderId) {
        MyorderExample example = new MyorderExample();
        example.createCriteria().andMyorderNumberEqualTo(OrderId);
        List<Myorder> myorderList = myorderMapper.selectByExample(example);
        if(myorderList.size()>0){
            return ServerResult.saveSussess(myorderList.get(0));
        }
        return ServerResult.saveFail(null);
    }

    @Override
    public ServerResult getAllReview() {
        ReviewExample reviewExample = new ReviewExample();
        reviewExample.createCriteria();
        reviewExample.setOrderByClause("review_date desc");
        List<Review> reviewList = reviewMapper.selectByExampleWithBLOBs(reviewExample);
//        System.out.println(reviewList);

        List<BasePojo> basePojoList = mongoTemplate.findAll(BasePojo.class,COLLECTIONNAME_REVIEW);
//        System.out.println(basePojoList);
        List<ReviewVO> reviewVOList = new ArrayList<>();

        //对mongodb内的数据进行查询
        if(basePojoList.size()>0) {
            List<BasePojo> basePojoList2 = new ArrayList<>();
            for (BasePojo basePojo : basePojoList) {
//                System.out.println(basePojo);
                int count=0;
                for (Review review : reviewList) {
//                    System.out.println(review);
                    System.out.println(review.getMyorderId());
                    System.out.println(basePojo.getOrderId());
                    if (!review.getMyorderId().equals(basePojo.getOrderId())) {
                        count++;
                    }
                }
                if(count>=reviewList.size())
                    basePojoList2.add(basePojo);
            }

            System.out.println("---------------------------------");
            System.out.println("base2:"+basePojoList2);

            for (BasePojo basePojo : basePojoList2) {
                ReviewVO reviewVO = new ReviewVO();
                Shop shop = shopMapper.selectByPrimaryKey(basePojo.getShopId());
                Member member = memberMapper.selectByPrimaryKey(basePojo.getMemberId());
                Car car = carMapper.selectByPrimaryKey(basePojo.getCarId());
                Review review = new Review();
                review.setMemberId(basePojo.getMemberId());
                review.setMyorderId(basePojo.getOrderId());
                review.setReviewDate(basePojo.getCreateTime());
                review.setStarShop(basePojo.getScore());
                review.setReviewContent(basePojo.getWrite());
                review.setSupportTotal(0);
                review.setCarId(basePojo.getCarId());
                review.setShopId(basePojo.getShopId());
                reviewVO.setReview(review);
                reviewVO.setCar(car);
                reviewVO.setMember(member);
                reviewVO.setShop(shop);
                reviewVOList.add(reviewVO);

            }

        }

        for(Review review : reviewList){
            ReviewVO reviewVO = new ReviewVO();
           Shop shop = shopMapper.selectByPrimaryKey(review.getShopId());
           Member member= memberMapper.selectByPrimaryKey(review.getMemberId());
           Car car = carMapper.selectByPrimaryKey(review.getCarId());
           reviewVO.setReview(review);
           reviewVO.setCar(car);
           reviewVO.setMember(member);
           reviewVO.setShop(shop);
           reviewVOList.add(reviewVO);

        }
        if(reviewVOList.size()>0){
            return ServerResult.getSussess(reviewVOList);
        }

        return  ServerResult.getFail(null);
    }

    @Override
    public List<Review> getById(String OrderName) {
        ReviewExample example = new ReviewExample();
        example.createCriteria().andMyorderIdEqualTo(OrderName);
        List<Review> reviewList = reviewMapper.selectByExample(example);
        if(reviewList.size()>0){
            return reviewList;
        }
        return null;
    }

    @Override
    public ServerResult save(Review review) {
        int rows = reviewMapper.insert(review);
        return  rows>0?ServerResult.saveSussess(null):ServerResult.saveFail(null);
    }

    @Override
    public Review update(Review Review) {
        System.out.println("review service  , Review:" + Review);
        int rows = reviewMapper.updateByPrimaryKeySelective(Review);
        if(rows >0){
            System.out.println("mysql  修改当前评论点赞数成功");
        }
        return Review;
    }


}
